package com.panopset.gui;

import static com.panopset.Util.dropExtension;
import static com.panopset.Util.dropPath;
import static com.panopset.Util.getReturnChar;
import static com.panopset.Util.isPopulated;
import static com.panopset.Util.log;
import static com.panopset.Util.saveBytesToFile;
import static com.panopset.Util.savePackageResource;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Vector;

import com.panopset.io.JarResources;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Collection of like small images.
 *
 * @author Karl Dinwiddie
 *
 */
public class SpritePackage {
    private final String jarPath;

    /**
     *
     * @param jarPath
     *            Path to jar containing .png images
     */
    public SpritePackage(String jarPath) {
        this.jarPath = jarPath;
    }

    @Override
    public String toString() {
        return getList();
    }

    private JarResources jarResources;

    public JarResources getJarResources() {
        if (jarResources == null) {
            jarResources = new JarResources(getJarFile());
        }
        return jarResources;
    }

    private Vector<String> resourceList;

    public Vector<String> getResourceList() {
        if (resourceList == null) {
            resourceList = new Vector<String>();
            try {
                for (String k : getJarResources().getSizes().keySet()) {
                    String name = dropPath(k);
                    if (isPopulated(name) && name.indexOf(".png") > -1) {
                        resourceList.add(dropExtension(name));
                    }
                }
            } catch (IOException e) {
                log(e);
            }
        }
        return resourceList;
    }

    private String listResources() {
        StringWriter sw = new StringWriter();
        for (String name : getResourceList()) {
            sw.append(name);
            sw.append(getReturnChar());
        }
        return sw.toString();
    }

    /**
     * Save all images to a directory
     *
     * @param targetPath
     *            path to directory, either full or relative to runtime.
     * @throws IOException
     */
    public void saveResourcesToDirectory(String targetPath) throws IOException {
        saveResourcesToDirectory(targetPath, null);
    }

    /**
     * Save all images to a directory
     *
     * @param targetPath
     *            path to directory, either full or relative to runtime.
     * @param filter Vector<String> only save those resources that are in this filter Vector.
     * Extensions ignored if provided.
     * @throws IOException
     */
    public void saveResourcesToDirectory(String targetPath, Vector<String> filter) throws IOException {
        for (String k : getJarResources().getSizes().keySet()) {
            String name = dropPath(k);
            if (isPopulated(name) && name.indexOf(".png") > -1) {
                if (filter == null || filter.contains(dropExtension(name)) || filter.contains(name)) {
                    saveBytesToFile(getJarResources().getResource(k), new File(
                            targetPath + "/" + name));
                }
            }
        }
    }

    File jarFile;

    /**
     * The package jar is saved in ${user.home}/.panopset for extraction.
     *
     * @return work File where jar is saved.
     */
    public File getJarFile() {
        if (jarFile == null) {
            String panopsetWorkDirectoryPath = System.getProperty("user.home")
                    + "/.panopset";
            try {
                jarFile = savePackageResource(jarPath, panopsetWorkDirectoryPath);
            } catch (IOException ex) {
                log("jarPath: " + jarPath);
                log("panopsetWorkDirectoryPath: " + panopsetWorkDirectoryPath);
                Logger.getLogger(SpritePackage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return jarFile;
    }

    private String list;

    /**
     *
     * @return list of names of each sprite .png image.
     */
    private String getList() {
        if (list == null) {
            list = listResources();
        }
        return list;
    }

}
